//
//  SaDBManager.m
//  saas
//
//  Created by fangyibao on 2019/10/15.
//  Copyright © 2019 fangmao. All rights reserved.
//

#import "SaDBManager.h"
#import <FMDB.h>
@interface SaDBManager()
@property (nonatomic, strong) FMDatabase *db;
@end
static SaDBManager *_ma;
@implementation SaDBManager
+(instancetype)shareManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _ma = [self new];
    });
    return _ma;
}
- (BOOL)creatTableWithName:(NSString *)tableN sql:(NSString *)sql{
    BOOL res = NO;
    if ([self _open]){
        res = [self.db executeUpdate:sql];
    }
    return res;
}
- (id)queryDataWithTableName:(NSString *)tableN sql:(NSString *)sql{
    if ([self _open]){
        FMResultSet *res = [self.db executeQuery:sql];
        return res;
    }
    return nil;
}
- (BOOL)insertDataWithTableName:(NSString *)tableN sql:(NSString *)sql{
    BOOL res = NO;
    if ([self _open]){
        res = [self.db executeUpdate:sql];
    }
    return res;
}
#pragma mark private
- (NSString *)dbPath{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    return [path stringByAppendingPathComponent:@"ZJ.sqlite"];
}
- (BOOL)_open{
    if ([self.db isOpen]){
        return [self.db isOpen];
    }else{
        return [self.db open];
    }
}
- (FMDatabase *)db{
    if (!_db) {
        _db = [FMDatabase databaseWithPath:[self dbPath]];
    }
    return _db;
}
@end
